gh-115692: Add tests to increase json coverage#115693
Conversation
| self.assertEqual( | ||
| result, | ||
| expect, | ||
| '{0!r} != {1!r} for {2}({3!r})'.format( |
There was a problem hiding this comment.
Any reason not to use an f-string here?
There was a problem hiding this comment.
This file was based on Lib/test/test_json/test_encode_basestring_ascii.py, so I'd left it the same.
But I've now updated the Lib/test/test_json/ files to use f-strings.
| ("NaN", "NAN"), | ||
| ]: | ||
| self.assertEqual( | ||
| self.loads(constant, parse_constant=str.upper), expected |
There was a problem hiding this comment.
You can simply use constant.upper() instead of expected.
There was a problem hiding this comment.
I generally prefer to have explicit expected results and avoid computing them:
https://testing.googleblog.com/2014/07/testing-on-toilet-dont-put-logic-in.html
But I can change it if you prefer.
There was a problem hiding this comment.
That's a good reason to do keep expected.
There was a problem hiding this comment.
To be consistent in avoiding computations in tests we should write this more explicitly:
self.assertEqual(self.loads("Infinity", parse_constant=str.upper),
"INFINITY")
self.assertEqual(self.loads("-Infinity", parse_constant=str.upper),
"-INFINITY")
self.assertEqual(self.loads("NaN", parse_constant=str.upper), "NAN")If you want to allow some computations, you can write it as:
for name in "Infinity", "-Infinity", "NaN":
self.assertEqual(self.loads(name, parse_constant=str.upper),
name.upper())which is also more breaf and simple that the current code.
There was a problem hiding this comment.
I prefer the existing version:
- it shows the explicit inputs and outputs
- avoids extra computations
- we can immediately see we're asserting the function under test in the exact same way each time
encukou
left a comment
There was a problem hiding this comment.
LGTM, as far as I see there's one nitpick remaining.
| ("NaN", "NAN"), | ||
| ]: | ||
| self.assertEqual( | ||
| self.loads(constant, parse_constant=str.upper), expected |
There was a problem hiding this comment.
That's a good reason to do keep expected.
Co-authored-by: Petr Viktorin <encukou@gmail.com>
|
Thanks, I'd missed Jelle's last comment. |
|
Thanks @hugovk for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12. |
(cherry picked from commit 8fc953f) Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
|
GH-117867 is a backport of this pull request to the 3.12 branch. |
…117867) Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Before
htmlcov-before.zip
After
htmlcov-after.zip
Lib/test/test_json/test_encode_basestring.py(testingjson.encoder.encode_basestring()) is based onLib/test/test_json/test_encode_basestring_ascii.py(testingjson.encoder.encode_basestring_ascii()).I also ran https://github.com/isidentical/teyit on the modified files to use more appropriate asserts with better error messages.
jsonmodule #115692